package com.example.ymm.modules1.service.impl.hl.ex;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.ex.ExPaper;
import com.example.ymm.modules1.entity.hl.ex.ExPublishGroup;
import com.example.ymm.modules1.entity.hl.ex.ExPublishPaper;
import com.example.ymm.modules1.mapper.ex.ExPublishGroupMapper;
import com.example.ymm.modules1.query.hl.ex.ExPublishGroupQueryCriteria;
import com.example.ymm.modules1.service.hl.ex.ExPaperService;
import com.example.ymm.modules1.service.hl.ex.ExPublishGroupService;
import com.example.ymm.modules1.service.hl.ex.ExPublishPaperService;
import com.example.ymm.modules1.socket.UserExWebSocket;
import com.example.ymm.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
@DS("slave2")
public class ExPublishGroupServiceImpl extends BaseServiceImpl<ExPublishGroupMapper, ExPublishGroup> implements ExPublishGroupService {

    @Autowired
    ExPaperService exPaperService;

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    private ExPublishPaperService exPublishPaperService;


    @Override
    public TableList<ExPublishGroup> list(ExPublishGroupQueryCriteria criteria, YmPageDto pageDto) {
        IPage<ExPublishGroup> page = new Page<ExPublishGroup>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(ExPublishGroup.class, criteria);
        predicate.orderByAsc("sort");
        baseMapper.selectPage(page, predicate);
        TableList<ExPublishGroup> tableList=new TableList(page);
        return tableList;
    }

    public List<ExPublishGroup> queryAll(ExPublishGroupQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(ExPublishGroup.class, criteria);
        predicate.orderByAsc("sort");
        List<ExPublishGroup> result =baseMapper.selectList(predicate);
        return result;
    }


    @Override
    public void savePublishGroup(ExPublishGroup exPublishGroup) {
        ExPublishGroup u_group=new ExPublishGroup();
        u_group.setPublishGroupId(exPublishGroup.getPublishGroupId());
        u_group.setLimitTime(exPublishGroup.getLimitTime());
        u_group.setCategoryName(exPublishGroup.getCategoryName());
        updateById(u_group);
    }

    @Override
    public void againPublishGroup(ExPublishGroup exPublishGroup) {
        ExPublishPaper publishPaper = exPublishPaperService.getById(exPublishGroup.getPublishPaperId());
        if(UtilValidate.isEmpty(publishPaper)){
            throw new YmException("查找不到发布试卷信息！");
        }
        exPublishGroup.setStatus(1);
        ExPaper exPaper = exPaperService.getByPublishPaperId(exPublishGroup.getPublishPaperId());

        Date date = new Date();
        exPublishGroup.setStartTime(new Timestamp(date.getTime()));
        Date end_date = DateKit.dateAdd(6, date, exPublishGroup.getLimitTime());
        exPublishGroup.setEndTime(new Timestamp(end_date.getTime()));
        exPublishGroup.setIzAgain(1);
        //加入到redis标识
        if(exPaper.getIsCoerce()==1){
            redisUtil.setIfAbsent(RedisKeyManager.team_paper_key+exPublishGroup.getPublishPaperId(),exPublishGroup.getPublishPaperId(),12, TimeUnit.HOURS);
        }

        saveOrUpdate(exPublishGroup);

        if(publishPaper.getStatus()!=1){
            publishPaper.setStatus(1);
            exPublishPaperService.saveOrUpdate(publishPaper);
        }
        //广播开始考试
        UserExWebSocket.broadcastStartPaper(publishPaper);

    }
}
